﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Framework.Verifications;

namespace Pixysoft.Framework.Sockets.Core
{
    class ActionHandlerCallbackFactory
    {
        private static int default_length_beanid = 3;

        private Dictionary<string, IActionHandlerCallback> handlers = new Dictionary<string, IActionHandlerCallback>();

        public bool RegisterCallback(int protocol, int action, IActionHandlerCallback command)
        {
            string beanId = "ID"
                    + StringCoder.AddZero(protocol.ToString(), default_length_beanid)
                    + StringCoder.AddZero(action.ToString(), default_length_beanid);

            if (handlers.ContainsKey(beanId))
                return false;

            handlers.Add(beanId, command);
            return true;
        }

        public IActionHandlerCallback TryGetCallback(int protocol, int action)
        {
            string beanId = "ID"
                    + StringCoder.AddZero(protocol.ToString(), default_length_beanid)
                    + StringCoder.AddZero(action.ToString(), default_length_beanid);

            if (!handlers.ContainsKey(beanId))
                return null;

            return handlers[beanId];
        }

        public void ClearCallbacks()
        {
            handlers.Clear();
        }
    }
}
